<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class BannerController extends BaseController {

    //轮播图列表
	public function index() {
        //筛选条件
        $data['banner_display'] = 1;

        $count = M('banner')->where($data)->count();
        $page = getpage($count,10);
        $banner_arr = M('banner')->where($data)->order('banner_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('banner_arr', $banner_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加轮播图
    public function banner_add(){
        if (I('post.action') == 'banner_add_action') {
            $this->banner_add_action();
            exit();
        }
        $data['midiclub_type'] = array('IN','1,3');
        $data['midiclub_display'] = 1;
        $club_arr = M('midiclub')->field('midiclub_id,midiclub_name')->where($data)->select();
        $this->assign('club_arr',$club_arr);
        $this->display();
    }

    //添加轮播图操作
    public function banner_add_action(){
        $_POST['banner_content'] = isset($_POST['banner_content'])?trim($_POST['banner_content']):'';
        if($_POST != array_filter($_POST)){
            $this->error('选项不能有空值');
        }
        $finfo = upload($_FILES['banner']);
        if ($finfo['status'] == 0) {
            if ($finfo['msg'] == 'nofile') {
                $this->error('轮播图未上传');
            }
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['banner_pic'] = $finfo['filepath'];
        }

        $filepath = './Html/banner/';
        create_folders($filepath);
        $filename = date('YmdHis',time()).rand(1000,9999).'.html';
        file_put_contents($filepath.$filename,$_POST['banner_content']);
        $_POST['banner_content'] = '/Html/banner/'.$filename;
        $_POST['banner_time'] = date('Y-m-d H:i:s',time());
        if(M('banner')->add($_POST)){
            $this->success('添加轮播图成功','index');
        }else{
            $this->error('添加轮播图失败');
        }

    }

    //修改轮播图信息
    public function banner_edit(){
        if (I('post.action') == 'banner_edit_action') {
            $this->banner_edit_action();
            exit();
        }
        $data['banner_id'] = I('get.banner_id');
        $banner_arr = M('banner')->where($data)->find();
        $banner_arr['banner_path'] = $banner_arr['banner_content'];
        $banner_arr['banner_content'] = file_get_contents(substr($banner_arr['banner_content'], 1));
        $this->assign('banner_arr',$banner_arr);
        $this->display();
    }

    //修改轮播图信息操作
    public function banner_edit_action(){

        $finfo = upload($_FILES['banner']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['banner_pic'] = $finfo['filepath'];
        }

        file_put_contents('.'.$_POST['banner_path'],$_POST['banner_content']);
        $_POST['banner_content'] = $_POST['banner_path'];
        unset($_POST['banner_path']);
        M('banner')->save($_POST);
        $this->success('操作完成','index');
    }

}